<?php
declare (strict_types = 1);
namespace app\api\controller;

use think\Request;
use app\BaseController;
use think\exception\ValidateException;

use app\api\validate\{UsersValidate, MakeValidate};
use app\api\model\{Users, Goods, TechnFiles, TechnCate, TechnTime, UserCard, Cate, TechnGoods};

class MakeController extends BaseController
{
    /**
	 * 预约商品列表
	 */
    public function getMakeGoodsLists()
    {
        if(request()->isPost()){
            $data = input('post.');

            try{
                validate(MakeValidate::class)->scene('goods_list')->check($data);
            }catch(ValidateException $e){
                $this->error_back($e->getError());
            }

            $goodsModel = new Goods();
            $cardModel = new UserCard();

            /** 查询可预约的商品ids */
            $goods_ids = $cardModel->getCol(['user_id' => $data['user_id'], 'admin_id' => $data['admin_id'], 'status' => 1], 'goods_id');

            $where[] = ['id', 'in', $goods_ids['data']];
            $where[] = ['status', '=', 1];
            $where[] = ['del_time', '=', 0];
            $where[] = ['cate_id', '=', $data['cate_id']];

            $field = 'id, title, subtitile, price, price_original, small_img';
            $list = $goodsModel->getList($where, 1, $data['curr'], $data['limit'], $field);

            if($list['code'] != 'ok'){
                $this->error_back($list['msg']);
            }

            $this->success_back('ok', $list['data']);
        }
    }

    /**
	 * 技师列表
	 */
    public function getTechnLists()
    {
        if(request()->isPost()){
            $data = input('post.');

            try{
                validate(MakeValidate::class)->scene('techn_list')->check($data);
            }catch(ValidateException $e){
                $this->error_back($e->getError());
            }

            $cateModel = new Cate();
            $usersModel = new Users();
            $technCateModel = new TechnCate();
            $technGoodsModel = new TechnGoods();

            // 获取技师关联商品列表
            $technGoodsLists = $technGoodsModel->getList(['goods_id' => $data['goods_id']], 0);

            if($technGoodsLists['data']->isEmpty()){
                $this->error_back('该服务项目无可选择技师，请选择其他项目！');
            }

            $technGoodsLists = $technGoodsLists['data']->toArray();
            $ids = array_column($technGoodsLists, 'user_id');

            // 获取技师列表
            $where[] = ['type', '=', 2];
            $where[] = ['id', 'in', $ids];
            $field = 'id, nickname, head_img';
            $list = $usersModel->getList($where, 1, $data['curr'], $data['limit'], $field);

            if($list['code'] != 'ok'){
                $this->error_back($list['msg']);
            }

            foreach($list['data'] as $k => $v){
                /** 查询技能列表 */
                $technCateLists = $technCateModel->getList(['user_id' => $v['id']], 0)['data'];

                /** 拼接技能 */
                $name = '';
                foreach($technCateLists as $key => $val){
                    $cate_info = $cateModel->getFind(['id' => $val['cate_id']]);

                    if($cate_info['code'] != 'ok'){
                        continue;
                    }

                    $name .= $cate_info['data']['name'] . ' ';
                }

                $list['data'][$k]['cate'] = $name;
            }

            $this->success_back('ok', $list['data']);
        }
    }

    /**
	 * 技师详情
	 */
    public function getTechnShow()
    {
        if(request()->isPost()){
            $data = input('post.');

            // 验证数据
            try{
                validate(UsersValidate::class)->scene('show')->check($data);
            }catch(ValidateException $e){
                $this->error_back($e->getError());
            }

            $usersModel = new Users();
            $goodsModel = new Goods();
            $technFilesModel = new TechnFiles();
            $technCateModel = new TechnCate();
            $technGoodsModel = new TechnGoods();

            $where = ['id' => $data['user_id']];
            $field = 'id, nickname, brief, head_img';
            $info = $usersModel->getFind($where, $field);

            if($info['code'] != 'ok'){
                $this->error_back($info['msg']);
            }

            // 获取技师关联类型列表
            $technCateLists = $technCateModel->getList(['user_id' => $data['user_id']], 0);

            if($technCateLists['data']->isEmpty()){
                $technCate = '';
            }else{
                $technCateListsArr = $technCateLists['data']->toArray();
                $ids = array_column($technCateListsArr, 'cate_id');
                $technCate = implode(',', $ids);
            }

            $info['data']['techn_cate'] = $technCate;

            // 获取擅长项目
            $technGoodsList = $technGoodsModel->getList(['user_id' => $data['user_id']], 1, 1, 3);

            if($technGoodsList['code'] != 'ok'){
                $info['data']['techn_goods_list'] = [];
            }else{
                $technGoodsListArr = $technGoodsList['data']->toArray();
                $ids = array_column($technGoodsListArr, 'goods_id');

                $goodsWhere[] = ['id', 'in', $ids];
                $goodsField = 'id, title, subtitile, price, price_original, small_img';
                $info['data']['techn_goods_list'] = $goodsModel->getList($goodsWhere, 0, 0, 0, $goodsField)['data'];
            }

            // 获取作品展示
            $info['data']['techn_files'] = $technFilesModel->getList(['user_id' => $data['user_id']], 0, 0, 0, 'id, file')['data'];

            $this->success_back('ok', $info['data']);
        }
    }

    /**
	 * 技师擅长项目
	 */
    public function getTechnGoodsLists()
    {
        if(request()->isPost()){
            $data = input('post.');

            // 验证数据
            try{
                validate(UsersValidate::class)->scene('show')->check($data);
            }catch(ValidateException $e){
                $this->error_back($e->getError());
            }

            $goodsModel = new Goods();
            $technGoodsModel = new TechnGoods();

            // 获取擅长项目
            $technGoodsList = $technGoodsModel->getList(['user_id' => $data['user_id']], 0);

            if($technGoodsList['code'] == 'fail'){
                $info['data']['techn_goods_list'] = [];
            }else{
                $technGoodsListArr = $technGoodsList['data']->toArray();
                $ids = array_column($technGoodsListArr, 'goods_id');

                $goodsWhere[] = ['id', 'in', $ids];
                $goodsField = 'id, title, subtitile, price, price_original, small_img';
                $list = $goodsModel->getList($goodsWhere, 0, 0, 0, $goodsField)['data'];
            }

            $this->success_back('ok', $list);
        }
    }

    /**
	 * 技师预约时间段列表
	 */
    public function getTechnTimeLists()
    {
        if(request()->isPost()){
            $data = input('post.');

            // 验证数据
            try{
                validate(UsersValidate::class)->scene('time')->check($data);
            }catch(ValidateException $e){
                $this->error_back($e->getError());
            }

            $technTimeModel = new TechnTime();

            if(strtotime(date('Y-m-d', time())) >= strtotime($data['time'])){
                // 获取今天的秒数
                $timeArr = getdate();
                $time = $timeArr['hours'] * 3600 + $timeArr['minutes'] * 60 + $timeArr['seconds'];
                
                $where[] = ['time_value', '>', $time];
            }else{
                $where = [];
            }
            
            $list = $technTimeModel->getList($where, 0);

            $this->success_back('ok', $list['data']);
        }
    }

    /**
	 * 获取店铺的所有员工
	 */
    public function getTechnListAll()
    {
        if(request()->isPost()){
            $data = input('post.');

            try{
                validate(MakeValidate::class)->scene('techn_list_all')->check($data);
            }catch(ValidateException $e){
                $this->error_back($e->getError());
            }

            $usersModel = new Users();

            // 获取技师列表
            $where[] = ['type', '=', 2];
            $where[] = ['admin_id', 'in', $data['admin_id']];
            $field = 'id, nickname';
            $list = $usersModel->getList($where, 0, 0, 0, $field);

            if($list['code'] != 'ok'){
                $this->error_back($list['msg']);
            }

            $this->success_back('ok', $list['data']);
        }
    }
}